﻿using AutoMapper;
using DFGK.IRepository;
using DFGK.IRepository.LY;
using DFGK.IService.LY;
using DFGK_WMS.Model.Models.LyModel;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static Microsoft.EntityFrameworkCore.DbLoggerCategory;

namespace DFGK.Service.LY
{
    public class SupplierReconciliationService : BaseService, ISupplierReconciliationService
    {
        private readonly ISupplierReconciliationRepository _supplierReconciliationRepository;
        private readonly IUnitOfWork unitOfWork;
        public SupplierReconciliationService(IMapper mapper, ISupplierReconciliationRepository supplierReconciliationRepository, IUnitOfWork unitOfWork) : base(mapper)
        {
            _supplierReconciliationRepository = supplierReconciliationRepository;
            this.unitOfWork = unitOfWork;
        }

        public bool AddOrModefiySupplierReconciliation(SupplierReconciliation model, out string errorMsg)
        {
            errorMsg = string.Empty;
            if (model.Id > 0)
            {
                _supplierReconciliationRepository.UpdEntity(model);
            }
            else
            {
                _supplierReconciliationRepository.AddEntity(model);

            }
            return unitOfWork.SaveChangesEntity();
        }

        public bool DelSupplierReconciliation(long supplierReconciliationId)
        {
            var Quote = _supplierReconciliationRepository.QueryEntityById(supplierReconciliationId);
            if (Quote == null)
            {
                return false;
            }
            _supplierReconciliationRepository.DelEntity(Quote);
            return unitOfWork.SaveChangesEntity();
        }

        public SupplierReconciliation? GetSupplierReconciliation(long supplierReconciliationId)
        {
            return _supplierReconciliationRepository.QueryEntityById(supplierReconciliationId);
        }

        public List<SupplierReconciliation> GetSupplierReconciliationData(string? name, DateTime? startDate, DateTime? endDate, out int totalCount, int pageIndex = 1, int pageSize = 15)
        {
            var list = _supplierReconciliationRepository.GetEntityAlls();
            if (!string.IsNullOrEmpty(name))
            {
                list = list.Where(d => d.SupplierName.Contains(name));
            }
            if (startDate.HasValue)
            {
                list = list.Where(r => r.ReconciliationDate >= startDate.Value);
            }

            if (endDate.HasValue)
            {
                list = list.Where(r => r.ReconciliationDate <= endDate.Value);
            }
            totalCount = list.Count();
            list = list.Skip((pageIndex - 1) * pageSize).Take(pageSize).OrderByDescending(d => d.CreateDate);
            return list.ToList();
        }

        public decimal GetTotalAmountBySupplier(string supplierName)
        {
            return _supplierReconciliationRepository.GetEntityAlls()
                 .Where(r => r.SupplierName == supplierName)
                 .Sum(r => r.Amount);
        }
    }
}
